package br.edu.univas.jobmanager.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import br.edu.univas.jobmanager.controller.ControllerRelatorioTarefas;
import br.edu.univas.jobmanager.entity.Categoria;
import br.edu.univas.jobmanager.entity.Estado;
import br.edu.univas.jobmanager.listener.FiltroRelatorioListener;
import br.edu.univas.jobmanager.utilitarios.JSearchTextField;
import br.edu.univas.jobmanager.utilitarios.Utilitarios;

public class PanelFiltrosTarefa extends JPanel{
	private static final long serialVersionUID = 1L;
	
	
	private JLabel dataLabelFiltro;
	private JLabel dataDeLabel;
	private JLabel dataALabel;
	private JFormattedTextField dataInicioField;
	private JFormattedTextField dataFimField;
	private JLabel filtroCategoriaLabel;
	private JComboBox filtroCategoriaBox;
	private JButton atualizarButton;
	private JLabel filtroStatusLabel;
	private JComboBox filtroStatusBox;
	private JSearchTextField filtroNomeField;
	private JButton filtrarPorDataButton;
	
	private ControllerRelatorioTarefas _controller;
	private FiltroRelatorioListener _listener;
	public PanelFiltrosTarefa(ControllerRelatorioTarefas _controller) {
		this._controller = _controller;
		_listener = _controller.getFiltrosListener();
		setLayout(new GridBagLayout());
		setBorder(BorderFactory.createTitledBorder("Filtros"));
		initialize();
	}


	private void initialize() {
		add(getDataLabelFiltro(),createConstraints(0, 0, 2));
		add(getDataDeLabel(),createConstraints(1, 0, 1));
		add(getDataInicioField(),createConstraints(1, 1, 1));
		add(getDataALabel(),createConstraints(1, 2, 1));
		add(getDataFimField(),createConstraints(1, 3, 1));
		add(getFiltrarPorDataButton(),createConstraints(1, 4, 1));
		add(getFiltroCategoriaLabel(),createConstraints(0, 5, 1));
		add(getFiltroStatusLabel(),createConstraints(0, 6, 1));
		add(getFiltroCategoriaBox(),createConstraints(1, 5, 1));
		add(getFiltroStatusBox(),createConstraints(1, 6, 1));
		add(getFiltroNomeField(),createConstraints(2, 0, 6));
		add(getAtualizarButton(),createConstraints(2, 6, 1));
		
	}


	private JLabel getDataLabelFiltro() {
		if(dataLabelFiltro == null){
			dataLabelFiltro = new JLabel("Filtrar por periodo");
		}
		return dataLabelFiltro;
	}


	private JLabel getDataDeLabel() {
		if(dataDeLabel == null){
			dataDeLabel = new JLabel("De: ");
		}
		return dataDeLabel;
	}


	private JLabel getDataALabel() {
		if(dataALabel == null){
			dataALabel = new JLabel(" à ");
		}
		return dataALabel;
	}


	private JFormattedTextField getDataInicioField() {
		if(dataInicioField == null){
			dataInicioField = new JFormattedTextField(Utilitarios.getMaskFormatter(Utilitarios.DATE_FORMAT));
		}
		return dataInicioField;
	}


	private JFormattedTextField getDataFimField() {
		if(dataFimField == null){
			dataFimField = new JFormattedTextField(Utilitarios.getMaskFormatter(Utilitarios.DATE_FORMAT));
		}
		return dataFimField;
	}


	private JLabel getFiltroCategoriaLabel() {
		if(filtroCategoriaLabel == null){
			filtroCategoriaLabel = new JLabel("Filtrar por categoria: ");
		}
		return filtroCategoriaLabel;
	}


	private JComboBox getFiltroCategoriaBox() {
		if(filtroCategoriaBox == null){
			filtroCategoriaBox = new JComboBox();
			filtroCategoriaBox.setModel(_controller.getCategoriaComboModel());
			filtroCategoriaBox.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					Categoria categoria = (Categoria) filtroCategoriaBox.getSelectedItem();
					_listener.filtrarPorCategoriaAction(categoria);
					
					
				}
			});
		}
		return filtroCategoriaBox;
	}


	private JLabel getFiltroStatusLabel() {
		if(filtroStatusLabel == null){
			filtroStatusLabel = new JLabel("Filtrar por status");
		}
		return filtroStatusLabel;
	}


	private JComboBox getFiltroStatusBox() {
		if(filtroStatusBox == null){
			filtroStatusBox = new JComboBox();
			filtroStatusBox.setModel(_controller.getModelComboStatus());
			filtroStatusBox.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					Estado estado = (Estado) filtroStatusBox.getSelectedItem();
					_listener.filtrarPorEstadoAction(estado);
					
				}
			});
		}
		return filtroStatusBox;
	}


	private JSearchTextField getFiltroNomeField() {
		if(filtroNomeField == null){
			filtroNomeField = new JSearchTextField();
			filtroNomeField.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					String texto = getFiltroNomeField().getText();
					_listener.filtrarPorNomeAction(texto);
					System.out.println(texto);
				}
			});
		}
		return filtroNomeField;
	}


	private JButton getFiltrarPorDataButton() {
		if(filtrarPorDataButton == null){
			filtrarPorDataButton = new JButton("Filtrar por data");
			filtrarPorDataButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm");
					Date dataInicial;
					Date dataFinal;
					try {
						dataInicial = sdf.parse(getDataInicioField().getText());
						dataFinal = sdf.parse(getDataFimField().getText());
						_listener.filtrarPorPeriodoAction(dataInicial, dataFinal);
					} catch (ParseException e1) {
						JOptionPane.showMessageDialog(null, "ERRO AO CONVERTER A DATA\n A DATA DEVE SER DIGITADA NO PADRAO DD/MM/AAAA HH:mm");
						e1.printStackTrace();
					}
					
				}
			});
			
		}
		return filtrarPorDataButton;
	}
	public GridBagConstraints createConstraints(int linha, int coluna, int gridWidth){
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(5, 5, 2, 5);
		gbc.gridx = coluna;
		gbc.gridy = linha;
		gbc.gridwidth = gridWidth;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		return gbc;
	}


	private JButton getAtualizarButton() {
		if(atualizarButton == null){
			atualizarButton = new JButton("Atualizar");
			atualizarButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					_listener.atualizar();
					
				}
			});
		}
		return atualizarButton;
	}
	
}
